package com.dc.ebond3.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Serializable;
/**
 * 执行linux命令
 * @author fengbo
 *
 */
public class LinuxCommandUtil implements Serializable {

	/**
	 * 执行命令后返回的结果
	 * @return
	 */
	public static String getExecCommandStr(String command){
		try {
		StringBuffer sb = new StringBuffer();
		Process process = execCommand(command);
		BufferedReader br=new BufferedReader(new InputStreamReader(process.getInputStream()));   
	    String inline;   
	
			while(null!=(inline=br.readLine()))
			{   
				 sb.append(inline);
				
			}
			br.close();
			return sb.toString();
		} catch (IOException e) {
			e.printStackTrace();
			return "";
		}

		
	}
	/**
	 * 执行命令
	 * @param command
	 * @return
	 */
	private static Process execCommand(String command){
		if(null==command||"".equals(command)){
			return null;
		}
		try {
			return Runtime.getRuntime().exec(command);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String comm = "free -m";
		String rs = getExecCommandStr(comm);
		//System.r
	}

}
